<?php
  include("DBConfig.class.php");
  include("GMapConfig.class.php");
  class DistrictMap{

    public static function getPovertyRatioMapByCode($code){


      //Query Province  $votename, $country, $filter
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  dis_id, 
                  dis_province_id,
                  dis_province_code,
                  dis_name,
                  dis_code,
                  dis_revenue,
                  dis_ratio_poverty,
                  dis_lat,
                  dis_lon
                FROM ".DBConfig::$dbname.".district
                WHERE dis_province_code = :code
                ORDER BY dis_lon DESC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();



        DistrictMap::renderPovertyRatioMap($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
      }
    }


    public static function renderPovertyRatioMap($result){
      include("gmap/districtPovertyRatioMap.php");
    }



    public static function getPovertyGapMapByCode($code){
      //Query Province  $votename, $country, $filter
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  dis_id, 
                  dis_province_id,
                  dis_province_code,
                  dis_name,
                  dis_code,
                  dis_revenue,
                  dis_gap_poverty,
                  dis_lat,
                  dis_lon
                FROM ".DBConfig::$dbname.".district
                WHERE dis_province_code = :code
                ORDER BY dis_lon DESC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        //var_dump($result);
        DistrictMap::renderPovertyGapMap($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
      }
    }


    public static function renderPovertyGapMap($result){
      include("gmap/districtPovertyGapMap.php");
    }




    public static function getPovertyViolenceMapByCode($code){
      //Query Province  $votename, $country, $filter
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  dis_id, 
                  dis_province_id,
                  dis_province_code,
                  dis_name,
                  dis_code,
                  dis_revenue,
                  dis_violence_poverty,
                  dis_lat,
                  dis_lon
                FROM ".DBConfig::$dbname.".district
                WHERE dis_province_code = :code
                ORDER BY dis_lon DESC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        //var_dump($result);
        DistrictMap::renderPovertyViolenceMap($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
      }
    }


    public static function renderPovertyViolenceMap($result){
      include("gmap/districtPovertyViolenceMap.php");
    }


    public static function getDefaultMapByCode($code){
      //Query Province  $votename, $country, $filter

      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  dis_id, 
                  dis_name,
                  dis_lat,
                  dis_lon
                FROM ".DBConfig::$dbname.".district
                WHERE dis_province_code = :code
                ORDER BY dis_id ASC";
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':code', $code, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();

        //var_dump($result);
        DistrictMap::renderDefaultMapByCode($result);

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $response['errorString'] = $e->getMessage();
          //$this->setProvinceResponse($response);
          //var_dump($response);
      }
    }


    public static function renderDefaultMapByCode($result){
      include("gmap/defaultDistrictMapByCode.php");
    }

  }
?>